const app = getApp();
Page({
  data: {
    sexList: [],
    placeList: [],
    // objPlaceList:[],
    marrysList: [],
    educationsList: [],
    cityList: [],
    regionList: [],
    moneyBegin: [2000, 4000, 6000, 8000, 10000, 12000, 14000, 16000, 18000, 20000, 30000, 50000, 100000],
    moneyEnd: [2000, 4000, 6000, 8000, 10000, 12000, 14000, 16000, 18000, 20000, 30000, 50000, 100000],
    ageBegin: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45],
    ageEnd: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45],
    heightBegin: [154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190],
    heightEnd: [154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190],
    req: {
      region: '',//地区
      place: '',//籍贯
      marrys: '',
      ageBegin: '',
      ageEnd: '',
      moneyBegin: '',
      moneyEnd: '',
      heightBegin: '',
      heightEnd: '',
      educations: ''
    },
    places: '',
    moneyBeginText: '',
    moneyEndText: '',
    ageBeginText: '',
    ageEndText: '',
    heightBeginText: '',
    heightEndText: '',

  },
  onLoad: function () {
    this.initPage();
  },
  initPage: function () {
    wx.showLoading({
      title: '数据加载中...',
    })
    //初始化数据
    var _this = this;
    app.getUserInfoSyn({"openId":wx.getStorageSync('openid')}).then(function () {
      var userInfo = wx.getStorageSync('userInfo');
      var mate = userInfo.mate;
      var req = _this.data.req;
      for (let k in req) {
        if (mate[k] == 0) {
          req[k] = '';
        } else {
          req[k] = mate[k];
        }
      }
      _this.setData({
        req: req,
        places: mate.places,
        moneyBeginText: mate.moneyBegin,
        moneyEndText: mate.moneyEnd,
        ageBeginText: mate.ageBegin,
        ageEndText: mate.ageEnd,
        heightBeginText: mate.heightBegin,
        heightEndText: mate.heightEnd,
      });

      app.doReq(app.globalData.COMMON.ADDRLIST, { parentId: 2 }, 'get', function (res) {
        wx.hideLoading();
        let data = res.data;
        if (data.code == 200) {
          let region = data.obj[2];
          console.log(region);
          app.doReq(app.globalData.DICTIONARY, { parentId: '6,8,975', }, 'get', function (res) {
            let data = res.data;
            if (data.code == 200) {
              let marrys = _this.removeUnused(data.obj[6]);
              let educations = _this.removeUnused(data.obj[8]);
              let place = data.obj[975];//籍贯
              _this.setData({
                placeList: place,
              });
              let lists = { marrys, educations, region };
              let req = _this.data.req
              for (let k in lists) {
                var tempList = [];
                for (let x in lists[k]) {
                  let obj = {};
                  obj.name = lists[k][x].name;
                  obj.value = lists[k][x].id;
                  var reqK = req[k] + ''.split(',');//转换为数组
                  if (reqK.indexOf(lists[k][x].id + '') >= 0) {
                    obj.checked = true;
                  } else {
                    obj.checked = false;
                  }
                  tempList.push(obj)
                }
                _this.setData({
                  [k + "List"]: tempList
                });
              }
            }
          });
        }
      });
    });
  },
  removeUnused: function (arr) {
    var _arr = [];
    for (var i = 0; i < arr.length; i++) {
      if (arr[i].name != '不透露') {
        _arr.push(arr[i]);
      }
    }
    return _arr;
  },
  whenChange: function (e) {
    console.log(e);
    let _this = this;
    let index = e.detail.value;
    let nodetype = e.currentTarget.dataset.type;
    if (nodetype == 'place') {
      let id = _this.data.placeList[index].id;
      let name = _this.data.placeList[index].name;
      _this.data.req.place = id;
      _this.setData({
        places: name,
      })
    } else {
      let v = _this.data[nodetype][index];
      _this.data.req[nodetype] = v;
      _this.setData({
        [nodetype + "Text"]: v
      })
    }
  },
  whenClick: function (e) {
    let v = e.currentTarget.dataset.v;
    let nodetype = e.currentTarget.dataset.type;
    let _this = this;
    let list = _this.data[nodetype + 'List'];
    for (let i of list) {
      if (v == i.value) {
        var attr = _this.data.req[nodetype];
        var newAttr = (attr + '').split(',');
        if (i.checked) {
          for (let x in newAttr) {
            if (newAttr[x] == v) {
             newAttr.splice(x, 1);
              break;
            }
          }
          i.checked = false;
        } else {
          newAttr.push(v);
          i.checked = true;
        }
        _this.data.req[nodetype] = newAttr.toString();
      }
    }
    _this.setData({
      [nodetype + 'List']: list
    });
  },
  doCancel: function () {
    wx.navigateBack({})
  },
  doSave: function () {
    let _this = this;
    //获得选择的标签
    let req = _this.data.req;
    req.educations.replace(/(^\,*)|(\,*$)/g, "");
    req.marrys.replace(/(^\,*)|(\,*$)/g, "");
    req.region.replace(/(^\,*)|(\,*$)/g, "");
    for (let key in req) {
      if (key != 'place' && key != 'region' && key != 'marrys' && key != 'educations') {
        if (req[key] === '') {
          wx.showToast({
            title: '请填写完整',
            icon: 'none'
          })
          return;
        }
      }
    }
    var reqData={}
    for (let key in req){  
      reqData['xgmate.'+key]=req[key];
    }
    reqData.id = app.globalData.userid;
   

    app.doReq(app.globalData.SERVICE.USERCENTER.MATE_CONDITION_SAVE, reqData, 'get',function (res) {
      var data = res.data;
      if (data.code == 200 && data.type == 'SUCCESS') {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          success:function(){
            var pages = getCurrentPages();
            var currentPage = pages[pages.length - 1];//当前页面
            var pervPagge = pages[pages.length - 2];//上个页面
            pervPagge.setData({
              reloadFlag: true,
            })
            wx.navigateBack({});
          }
        });
        app.getUserInfoSyn({"openId":wx.getStorageSync('openid')})
      }
    });
  },
});